package org.example.Forms;

import org.example.Controller.FormManagerNode;
import org.example.Controller.RunTask;
import org.example.Tool.Component.CheckCard;
import org.example.Tool.Component.ContentPanel;
import org.example.Tool.Component.tContentPanel.MonitorPanel;
import org.example.Tool.Component.tContentPanel.EmptyPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.function.Function;


public class MainForm implements IForm {
    protected JFrame frame;

    protected ContentPanel mainPanel;

    private static final ContentPanel emptyPanel;

    protected JPanel upPanel;

    protected JScrollPane SidePanel;

    public static FormManagerNode MAIN_MANAGER = new FormManagerNode();

    public static ArrayList<RunTask> MAIN_TASK = new ArrayList<>();

    public static int MainFormHeight = 800;

    public static int MainFormWidth = 1200;

    public static Dimension startPoint = new Dimension(100, 40);

    static {
        emptyPanel = new EmptyPanel(
                new Dimension(
                        MainForm.MainFormWidth - MainForm.startPoint.width,
                        MainForm.MainFormHeight - MainForm.startPoint.height
                ),
                Toolkit.getDefaultToolkit().getImage(
                ContentPanel.class.getResource("/Image/R-C.png")
        ));
    }

    public MainForm() {
        MAIN_MANAGER.init(this);
        frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        componentInit();
    }

    protected void switchMainPanel(Function<Dimension,? extends ContentPanel> factory) {
        Dimension csize = mainPanel.getSize();
        frame.getContentPane().remove(mainPanel);
        mainPanel = factory.apply(csize);
        mainPanel.setVisible(true);
        frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
        System.out.println(mainPanel.getSize().toString() + "  " + csize.toString());
        frame.pack();
    }

    private void componentInit() {
        JMenuBar jMenuBar = new JMenuBar();
        JMenu menu = new JMenu("总项");
        JMenuItem menuItem = new JMenuItem("创建新任务");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                JDialog dialog = new JDialog(frame, "提示");
                System.out.println("add task");
                dialog.setSize(new Dimension(200, 300));
                dialog.setLayout(new FlowLayout(FlowLayout.CENTER));
                dialog.getContentPane().add(new JLabel("添加成功"));
                dialog.setVisible(true);
                RunTask task = new RunTask("监视" + MainForm.MAIN_TASK.size());
                task.type = RunTask.TaskType.DataWatching;
                MainForm.MAIN_TASK.add(task);
                new Thread(task).start();
                if(mainPanel instanceof MonitorPanel)
                    ((MonitorPanel)mainPanel).readTask();
            }
        });

        menu.add(menuItem);
        jMenuBar.add(menu);
        frame.setJMenuBar(jMenuBar);

        mainPanel = emptyPanel;

        JComboBox<String> comboBox = new JComboBox<>();

        comboBox.addItem("展示1");
        upPanel = new JPanel(new FlowLayout());
        upPanel.setSize(new Dimension(MainFormWidth, startPoint.height));
        upPanel.add(comboBox);

        {
            JPanel childPanel = new JPanel();
            childPanel.setLayout(null);
            childPanel.setPreferredSize(new Dimension(startPoint.width, MainFormHeight));


            CheckCard card = new CheckCard("数据监视");
            card.setClickEvent(()->switchMainPanel(MonitorPanel::new));
            CheckCard card2 = new CheckCard("??");
            childPanel.add(card);
            childPanel.add(card2);
            card.setBounds(1,1,startPoint.width - 2, 50);
            card2.setBounds(1,56,startPoint.width - 2, 50);
            SidePanel = new JScrollPane(childPanel);
        }


        frame.setLayout(new BorderLayout());
        Container icontainer = frame.getContentPane();
        icontainer.add(mainPanel, BorderLayout.CENTER);
        icontainer.add(upPanel, BorderLayout.NORTH);
        icontainer.add(SidePanel, BorderLayout.WEST);
        frame.pack();
    }

//    public AbstractForm.Builder createNewForm(){
//        return new AbstractForm.Builder(MAIN_MANAGER);
//    }

    @Override
    public void show() {
        frame.setVisible(true);
    }

    @Override
    public String getSystemId(){
        return "MAIN";
    }

}
